<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="RS Image Format1. RS Image下图是一幅遥感图像显示的示例。图像窗口中，显示的图像是矩形区域，对应的数据为R-Band3， G-Band4， B-Band5，所以图像可以描述为三维矩阵（长*宽*高）。当图像逐步放大时，从右下方的Zoom窗口中可见，图像开始出现“马赛克”效应，即每一个像素点显示的够大，以至于像素间呈现了差异，这形象地揭示了图像和矩阵的联系。  2. RS Im">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://example.com/2023/06/05/docs/RSImage/RSImageFormat/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="RS Image Format1. RS Image下图是一幅遥感图像显示的示例。图像窗口中，显示的图像是矩形区域，对应的数据为R-Band3， G-Band4， B-Band5，所以图像可以描述为三维矩阵（长*宽*高）。当图像逐步放大时，从右下方的Zoom窗口中可见，图像开始出现“马赛克”效应，即每一个像素点显示的够大，以至于像素间呈现了差异，这形象地揭示了图像和矩阵的联系。  2. RS Im">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://i.loli.net/2021/07/04/jwPr5FKSGAegkOf.png">
<meta property="og:image" content="https://i.loli.net/2021/07/04/8dVyBcmqbJEUuLR.png">
<meta property="article:published_time" content="2023-06-05T15:25:53.701Z">
<meta property="article:modified_time" content="2022-06-11T03:19:44.048Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/07/04/jwPr5FKSGAegkOf.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-docs/RSImage/RSImageFormat" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/06/05/docs/RSImage/RSImageFormat/" class="article-date">
  <time class="dt-published" datetime="2023-06-05T15:25:53.701Z" itemprop="datePublished">2023-06-05</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="RS-Image-Format"><a href="#RS-Image-Format" class="headerlink" title="RS Image Format"></a>RS Image Format</h1><h2 id="1-RS-Image"><a href="#1-RS-Image" class="headerlink" title="1. RS Image"></a>1. RS Image</h2><p>下图是一幅遥感图像显示的示例。图像窗口中，显示的图像是矩形区域，对应的数据为R-Band3， G-Band4， B-Band5，所以图像可以描述为三维矩阵（长*宽*高）。当图像逐步放大时，从右下方的Zoom窗口中可见，图像开始出现“马赛克”效应，即每一个像素点显示的够大，以至于像素间呈现了差异，这形象地揭示了图像和矩阵的联系。</p>
<p><img src="https://i.loli.net/2021/07/04/jwPr5FKSGAegkOf.png" alt="image-20210704093148250"></p>
<h2 id="2-RS-Image-File"><a href="#2-RS-Image-File" class="headerlink" title="2. RS Image  File"></a>2. RS Image  File</h2><p>既然多波段的遥感图像是三维矩阵，那么我们又是如何存储的呢？数据存储在外存（硬盘）上时，只有一种存储方式——顺序存储。下图是上图显示图像的数据存储文件。从左侧数据地址可知，数据是按字节依次存放的，从地址0x00000000开始，每一行存16个字节，第2行首地址即为：0x00000010，依此类推。</p>
<p><img src="https://i.loli.net/2021/07/04/8dVyBcmqbJEUuLR.png" alt="image-20210704101855309"></p>
<p>如果我们拿到上述的图像存储文件，我们能否将图像转变为三维矩阵呢？显然还不能，因为我们并不知道图像的长、宽、高以及每个像素存储的字节数（数据类型）。若要正确转换为矩阵，此时我们就需要借助一个对图像数据进行描述的文件，这种描述数据的数据，我们称之为<strong>元数据</strong>。对应其上的元数据文件如下所示：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">ENVI</span><br><span class="line">description = &#123;</span><br><span class="line">  Canon City, Colorado, Landsat TM, Calibrated to Reflectance &#125;</span><br><span class="line">samples = 640</span><br><span class="line">lines   = 400</span><br><span class="line">bands   = 6</span><br><span class="line">header offset = 0</span><br><span class="line">file type = ENVI Standard</span><br><span class="line">data type = 1</span><br><span class="line">interleave = bsq</span><br><span class="line">sensor type = Landsat TM</span><br><span class="line">wavelength units = Micrometers</span><br><span class="line">z plot range = &#123;0.00, 100.00&#125;</span><br><span class="line">z plot titles = &#123;Wavelength, Reflectance&#125;</span><br><span class="line">default stretch = 2.0% linear</span><br><span class="line">band names = &#123;</span><br><span class="line"> TM Band 1, TM Band 2, TM Band 3, TM Band 4, TM Band 5, TM Band 7&#125;</span><br><span class="line">wavelength = &#123;</span><br><span class="line"> 0.48500, 0.56000, 0.66000, 0.83000, 1.65000, 2.21500&#125;</span><br></pre></td></tr></table></figure>

<p>从元数据中，我们可以读取到samples - 列，lines - 行, bands &#x3D; 波段, data type - 数据类型，interleave-间隔方式等元数据。</p>
<h2 id="3-RS-Image-Format"><a href="#3-RS-Image-Format" class="headerlink" title="3. RS Image Format"></a>3. RS Image Format</h2><p>在三维矩阵写入到图像文件中时，根据数据间隔(interleave)方式不同，我们将数据分为按波段间隔-BSQ， 按行间隔-BIL和按点间隔-BIP，可以简单记为：点-BIP&#x2F;线-BIL&#x2F;面-BSQ。何为间隔方式呢，就是三维矩阵中的元素排列的方式，以三维数组A[i][j][k]为例，i-波段数bands，j-行数lines，k-列数samples，则：</p>
<ul>
<li><p>BSQ 排列顺序为：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">第<span class="number">1</span>个波段：A[<span class="number">0</span>][<span class="number">0</span>][<span class="number">0</span>],..., A[<span class="number">0</span>][lines<span class="number">-1</span>][samples<span class="number">-1</span>],</span><br><span class="line">第<span class="number">2</span>个波段：A[<span class="number">1</span>][<span class="number">0</span>][<span class="number">0</span>],..., A[<span class="number">1</span>][lines<span class="number">-1</span>][samples<span class="number">-1</span>],</span><br><span class="line">第.个波段：A[...][<span class="number">0</span>][<span class="number">0</span>],..., A[...][lines<span class="number">-1</span>][samples<span class="number">-1</span>],</span><br><span class="line">第b个波段：A[b<span class="number">-1</span>][<span class="number">0</span>][<span class="number">0</span>],..., A[b<span class="number">-1</span>][lines<span class="number">-1</span>][samples<span class="number">-1</span>]</span><br></pre></td></tr></table></figure>
</li>
<li><p>BIL 排列顺序为：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">第<span class="number">1</span>个波段第<span class="number">1</span>行：A[<span class="number">0</span>][<span class="number">0</span>][<span class="number">0</span>],..., A[<span class="number">0</span>][<span class="number">0</span>][samples<span class="number">-1</span>],</span><br><span class="line">...,</span><br><span class="line">第<span class="number">1</span>个波段第l行：A[<span class="number">0</span>][l<span class="number">-1</span>][<span class="number">0</span>],..., A[<span class="number">0</span>][l<span class="number">-1</span>][samples<span class="number">-1</span>],</span><br><span class="line">...,</span><br><span class="line"></span><br><span class="line">第<span class="number">2</span>个波段第<span class="number">1</span>行：A[<span class="number">1</span>][<span class="number">0</span>][<span class="number">0</span>],..., A[<span class="number">1</span>][<span class="number">0</span>][samples<span class="number">-1</span>],</span><br><span class="line">...,</span><br><span class="line">第<span class="number">2</span>个波段第l行：A[<span class="number">1</span>][l<span class="number">-1</span>][<span class="number">0</span>],..., A[<span class="number">1</span>][l<span class="number">-1</span>][samples<span class="number">-1</span>],</span><br><span class="line">...,</span><br><span class="line"></span><br><span class="line">第b个波段第<span class="number">1</span>行：A[b<span class="number">-1</span>][<span class="number">0</span>][<span class="number">0</span>],..., A[<span class="number">1</span>][<span class="number">0</span>][samples<span class="number">-1</span>],</span><br><span class="line">...,</span><br><span class="line">第b个波段第l行：A[b<span class="number">-1</span>][l<span class="number">-1</span>][<span class="number">0</span>],..., A[b<span class="number">-1</span>][l<span class="number">-1</span>][samples<span class="number">-1</span>]</span><br></pre></td></tr></table></figure>
</li>
<li><p>BIP 排列顺序为：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">第<span class="number">1</span>个波段第<span class="number">1</span>行第<span class="number">1</span>列：A[<span class="number">0</span>][<span class="number">0</span>][<span class="number">0</span>],</span><br><span class="line">...,</span><br><span class="line">第b个波段第<span class="number">1</span>行第<span class="number">1</span>列：A[b<span class="number">-1</span>][<span class="number">0</span>][<span class="number">0</span>],</span><br><span class="line">...,</span><br><span class="line"></span><br><span class="line">第<span class="number">1</span>个波段第<span class="number">1</span>行第<span class="number">2</span>列：A[<span class="number">0</span>][<span class="number">0</span>][<span class="number">1</span>],</span><br><span class="line">...,</span><br><span class="line">第b个波段第<span class="number">1</span>行第<span class="number">2</span>列：A[b<span class="number">-1</span>][<span class="number">0</span>][<span class="number">1</span>],</span><br><span class="line">...,</span><br><span class="line"></span><br><span class="line">第<span class="number">1</span>个波段第<span class="number">1</span>行第s列：A[<span class="number">0</span>][<span class="number">0</span>][s<span class="number">-1</span>],</span><br><span class="line">...,</span><br><span class="line">第b个波段第<span class="number">1</span>行第s列：A[b<span class="number">-1</span>][<span class="number">0</span>][s<span class="number">-1</span>],</span><br><span class="line"></span><br><span class="line">...,</span><br><span class="line">第<span class="number">1</span>个波段第<span class="number">2</span>行第<span class="number">1</span>列：A[<span class="number">0</span>][<span class="number">1</span>][<span class="number">0</span>],</span><br><span class="line">...,</span><br><span class="line">第b个波段第l行第s列：A[b<span class="number">-1</span>][l<span class="number">-1</span>][s<span class="number">-1</span>]</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="4-Read-RS-Image-File-to-Image-Matrix"><a href="#4-Read-RS-Image-File-to-Image-Matrix" class="headerlink" title="4. Read RS Image File to Image Matrix"></a>4. Read RS Image File to Image Matrix</h2><p>在已知了samples, lines, bands, datatype, interleave之后，用程序来实现读取就变的比较容易了。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//以数据类型为unsigned char为例</span></span><br><span class="line"><span class="comment">// 分配3维数组，存放图像文件</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span>*** pImg = <span class="keyword">new</span> <span class="type">unsigned</span> <span class="type">char</span>**[nbands];</span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;nbands; ++i)</span><br><span class="line">    pImg[i] = <span class="keyword">new</span> <span class="type">unsigned</span> <span class="type">char</span>* [nlines];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;nbands; ++i)</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j=<span class="number">0</span>; j&lt;nlines; ++j)</span><br><span class="line">        pImg[i][j] = <span class="keyword">new</span> <span class="type">unsigned</span> <span class="type">char</span>[nsamples];</span><br><span class="line"></span><br><span class="line"><span class="function">ifstream	<span class="title">ifs</span><span class="params">(<span class="string">&quot;test.img&quot;</span>, ios::binary)</span></span>;</span><br><span class="line"><span class="comment">// BSQ读取</span></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;nbands; ++i)</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j=<span class="number">0</span>; j&lt;nlines; ++j)</span><br><span class="line">        ifs.<span class="built_in">read</span>(<span class="keyword">reinterpret_cast</span>&lt;<span class="type">char</span>*&gt;pImg[i][j], <span class="built_in">sizeof</span>(<span class="type">unsigned</span> <span class="type">char</span>)*nsamples);</span><br><span class="line"></span><br><span class="line"><span class="comment">//BIL读取</span></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> j=<span class="number">0</span>; j&lt;nlines; ++j)</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;nbands; ++i)</span><br><span class="line">        ifs.<span class="built_in">read</span>(<span class="keyword">reinterpret_cast</span>&lt;<span class="type">char</span>*&gt;pImg[i][j], <span class="built_in">sizeof</span>(<span class="type">unsigned</span> <span class="type">char</span>)*nsamples);</span><br><span class="line"></span><br><span class="line"><span class="comment">//BIP读取</span></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> j=<span class="number">0</span>; j&lt;nlines; ++j)</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> k=<span class="number">0</span>; k&lt;nsamples; ++k)</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;nbands; ++i)</span><br><span class="line">            ifs.<span class="built_in">read</span>(<span class="keyword">reinterpret_cast</span>&lt;<span class="type">char</span>*&gt;&amp;pImg[i][j][k], <span class="built_in">sizeof</span>(<span class="type">unsigned</span> <span class="type">char</span>));</span><br><span class="line"></span><br><span class="line">ifs.<span class="built_in">close</span>();</span><br></pre></td></tr></table></figure>

<h2 id="5-Write-Image-Matrix-to-RS-Image-File"><a href="#5-Write-Image-Matrix-to-RS-Image-File" class="headerlink" title="5. Write Image Matrix to RS Image File"></a>5. Write Image Matrix to RS Image File</h2><p>写文件，与读文件类似。把<code>ifs.read() --&gt; ifs.write</code>即可。</p>
<h2 id="References"><a href="#References" class="headerlink" title="References"></a>References</h2>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/06/05/docs/RSImage/RSImageFormat/" data-id="clij09mc8000dc8n57wqgblpg" data-title="" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/06/05/docs/RSImage/Statistics/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2023/06/05/docs/RSImage/RSCmdManual/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/06/">June 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/06/05/png/RSCmdManual/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/Subject/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/Statistics/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/RSImageFormat/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/06/05/docs/RSImage/RSCmdManual/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>